<?php
/**
 * Created by kavience.
 * web: www.kavience.com
 * Date: 2017/12/28
 * Time: 9:12
 */

namespace app\blog\controller;


use app\blog\model\NavModel;
use cmf\controller\HomeBaseController;
use think\Db;

/**
 * 基本控制器，获取网页导航、友链、广告等全局信息
 * Class BaseController
 * @package app\blog\controller
 */
class BaseController extends HomeBaseController
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
//        halt(self::getBlogInfo());
        $this->assign('blogInfo',self::getBlogInfo());
//        halt(self::getAllNav());
        $this->assign('navs',self::getAllNav());
//        halt(self::getFriendLinks());
        $this->assign('friendLinks',self::getFriendLinks());
//        halt(self::getAds());
        $this->assign('ads',self::getAds());
//        halt(self::getFreshArticle());
        $this->assign('freshArticles',self::getFreshArticle());

//        halt(self::getHotArticle());
        $this->assign('hotArticles',self::getHotArticle());

//        halt(self::getArchive());
        $this->assign('archives',self::getArchive());
    }

    /**
     * 获取导航
     * @return array
     */
    public static function getAllNav()
    {
        $allNav = NavModel::getAllNav();
        $allNav = $allNav->toArray();
        $newNav = self::make_tree($allNav,'id','parent_id');
        return $newNav;
    }

    /**
     * 获取网站信息
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getBlogInfo()
    {
        $blogInfo = Db::table('cmf_blog_info')->find(1);
        return $blogInfo;
    }

    /**
     * 获取网站广告
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getAds()
    {
        $ads = Db::table('cmf_blog_ad')->order('list_order','desc')->select();
        return $ads;
    }
    /**
     * 获取友情链接
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getFriendLinks()
    {
        $friendLinks = Db::table('cmf_blog_links')->order('list_order','desc')->select();
        return $friendLinks;
    }

    /**
     * 获取最新文章
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getFreshArticle()
    {
        $freshArticle = Db::table('cmf_blog_article')->order('create_time','desc')->limit(5)->select();
        return $freshArticle;
    }

    /**
     * 获取最热文章
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getHotArticle()
    {
        $hotArticle = Db::table('cmf_blog_article')->order('read_times','desc')->limit(5)->select();
        return $hotArticle;
    }

    /**
     * 返回文章归档
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getArchive()
    {
        $field = [
            'count(id)'=>'num',
            'FROM_UNIXTIME(create_time,\'%Y\')'=>'year',
            'FROM_UNIXTIME(create_time,\'%m\')'=>'month',
            'FROM_UNIXTIME(create_time,\'%Y年%m月\')'=>'archive',
        ];
        $archive = Db::table('cmf_blog_article')->field($field)->group('archive')->order('create_time','desc')->select();
//        halt($archive);
        return $archive;
    }

    /**
     * 无限极分类
     * @param        $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int    $root
     * @return array
     */
    public static function make_tree($list,$pk='id',$pid='parent_id',$child='son',$root=0){
        $tree=array();
        $packData=array();
        foreach ($list as $data) {
            //转换为带有主键id的数组
            $packData[$data[$pk]] = $data;
            $packData[$data[$pk]]['hasSon'] = 'false';
            //$packData[1]=$data; $packData[2]=$data
        }
        foreach ($packData as $key =>$val){
            if($val[$pid]==$root){ //代表跟节点
                $tree[]= &$packData[$key];
            }else{
                //找到其父类
                $packData[$val[$pid]]['hasSon'] = 'true';
                $packData[$val[$pid]][$child][]= &$packData[$key];
            }
        }
        return $tree;
    }
}